# Contributor: August Klein <amatcoder@gmail.com>
# Maintainer: Simon Zeni <simon@bl4ckb0ne.ca>
pkgname=sdl2
pkgver=2.30.9
pkgrel=0
pkgdesc="development library designed to provide low level access to audio, keyboard, mouse, joystick and graphics"
url="https://www.libsdl.org"
arch="all"
license="Zlib"
depends_dev="$pkgname mesa-dev"
makedepends="
	alsa-lib-dev
	cmake
	eudev-dev
	jack-dev
	libsamplerate-dev
	libx11-dev
	libxcursor-dev
	libxext-dev
	libxi-dev
	libxkbcommon-dev
	libxrandr-dev
	libxrender-dev
	libxscrnsaver-dev
	libusb-dev
	pipewire-dev
	pulseaudio-dev
	samurai
	wayland-dev
	wayland-protocols
	$depends_dev
	"
subpackages="$pkgname-dbg $pkgname-doc $pkgname-dev"
source="https://www.libsdl.org/release/SDL2-$pkgver.tar.gz"
builddir="$srcdir/SDL2-$pkgver"

# secfixes:
#   2.0.18-r0:
#     - CVE-2021-33657
#   2.0.10-r0:
#     - CVE-2019-7572
#     - CVE-2019-7573
#     - CVE-2019-7574
#     - CVE-2019-7575
#     - CVE-2019-7576
#     - CVE-2019-7578
#     - CVE-2019-7635
#     - CVE-2019-7636
#     - CVE-2019-7637
#     - CVE-2019-7638

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		cmake_crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	CFLAGS="$CFLAGS -flto=auto -ffat-lto-objects" \
	cmake -B build -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_JOB_POOLS=thr=3 \
		-DCMAKE_JOB_POOL_LINK=thr \
		-DBUILD_SHARED_LIBS=True \
		-DSDL_TEST=ON \
		-DSDL_TESTS="$(want_check && echo ON || echo OFF)" \
		-DSDL_ALSA=ON \
		-DSDL_JACK=ON \
		-DSDL_CLOCK_GETTIME=ON \
		-DSDL_ARTS=OFF \
		-DSDL_ESD=OFF \
		-DSDL_NAS=OFF \
		-DSDL_RPATH=OFF \
		-DSDL_X11=ON \
		-DSDL_DIRECTFB=OFF \
		-DSDL_WAYLAND=ON \
		-DSDL_STATIC=ON \
		-DSDL_SHARED=ON \
		$cmake_crossopts
	ninja -C build
}

check() {
	case "$CARCH" in
		# Test is broken https://github.com/libsdl-org/SDL/issues/9219
		ppc64le) ctest --test-dir build -E testautomation;;
		*) ctest --test-dir build;;
	esac
}

package() {
	DESTDIR="$pkgdir" ninja -C build install
}

dev() {
	default_dev

	# move back dev symlink into main package so things can dlopen it
	# libSDL2.so -> libSDL2-2.0.so -> libSDL2-2.0.so.version
	mv "$subpkgdir"/usr/lib/libSDL2.so "$subpkgdir"/usr/lib/libSDL2-2.0.so \
		"$pkgdir"/usr/lib
}

sha512sums="
30dfa86fcced174fef0ed78ffa53476a31765e19cdcdf8233ab92876445b4dedaa758fc42a3ec332324d13faa2daafcadcc44fc0f536a2969ef836162ec3cd36  SDL2-2.30.9.tar.gz
"
